#coding=utf8
#_author_: chanceyang
#date:2020/9/2
import os
import sqlite3
from types import CodeType, FunctionType

import arrow
import pymysql
import pytest
from arrow import Arrow
from createDataByInsert.RedisInsert import RedisInsert
from createDataByInsert.dataDecorater import dataBaseDec, dataSysDec, sqliteDec,redisDataSysDec
from createDataByInsert.excelUtils import strToList

from dataForTest.dbCommon import reqDB
from dataForTest.dataMethod import *
from redisKeys.redis_keys import MOCK_RANK
from utils.common_sydr import *
from utils.environments_json import  json_data
from redis import Redis
from utils.common_dcc import  create_all_data, delete_all_data, delete_exam_info
from setupDemo import login_session_mid, delete_user_mid
from createDataByInsert.ByInsertClass import create_data, InsertClass
from utils.path_generator import filePath
import  requests
# mysql数据库链接测试固件
@pytest.fixture(scope="session")
def mysqlConnect():
    mysql_json = json_data.get("mysql")
    server = mysql_json.get("server")
    port = mysql_json.get("port")
    user = mysql_json.get("username")
    passwd = mysql_json.get("password")
    database = json_data.get(json_data.get("current_env") + "_mysql").get(json_data.get("default_project"))
    db = pymysql.connect(host=server, port=port, user=user, password=passwd, db=database, charset='utf8',
                         autocommit=True)
    cursor = db.cursor()
    yield cursor
    cursor.close()
    db.close()

# redis数据库链接测试固件
@pytest.fixture(scope="session")
@dataBaseDec(json_data,"redis")
def redisConnect()->Redis:
    ...

# 测试数据创建、删除固件
@pytest.fixture(scope="session")
def data_process():
    data_dict = create_all_data()
    yield data_dict
    delete_all_data(data_dict)

# dcc项目中台，登录专用固件
@pytest.fixture(scope="session")
def login_session():
    r, user_id = login_session_mid()
    yield r, user_id
    delete_user_mid(user_id=user_id)
    delete_exam_info(user_id=user_id)

# dcc项目中台，登录专用固件，用户二
@pytest.fixture(scope="session")
def login_session_two():
    r, user_id = login_session_mid()
    yield r, user_id
    delete_user_mid(user_id=user_id)
    delete_exam_info(user_id=user_id)

# dcc项目中台，登录专用固件，用户三（因为模考大赛定时任务、章节练习、错误本三个模块的题目作答提交相互影响，所以三个模块都独
# 立一个登录的固件，其余公用一个，这样才能把固件设为session范围）
@pytest.fixture(scope="session")
def login_session_thr():
    r, user_id = login_session_mid()
    yield r, user_id
    delete_user_mid(user_id=user_id)
    delete_exam_info(user_id=user_id)




# 测试数据准备,调用接口生成数据
@pytest.fixture(scope="session")
def topicDataGenerator(androidAndWebSession,mysqlConnect_sydr,redisConnect,sqliteConnect):
    rDB = reqDB(sqliteConnect, "relation_table", mysqlConnect_sydr[1])
    rDB.replaceDataAndReq()
    yield rDB.tableIdsDic
    rDB.deleData()
    deleUserExam(mysqlConnect_sydr, rDB.tableIdsDic, redisConnect,sessionList)


# 用户中心的sqlite数据库连接
@pytest.fixture(scope="session")
@sqliteDec(filePath.sqlite_path + "userCenter.db")
def userCenterSqliteCon()->Connection:
    ...


# 用户中心的mysql数据库连接
@pytest.fixture(scope="session")
@dataBaseDec(json_data,"mysql",project="userCenter")
def userCenterMysqlCon():
    ...

# 测试数据准备,在中心数据库插入数据
@dataSysDec("session",filePath.sqlite_path + "userCenterTablesJSON.json","cpaSydr","userCenter")
def userCenterInsert(userCenterMysqlCon,userCenterSqliteCon):
    ...

# mysql数据库链接测试固件,不会自动commit
@pytest.fixture(scope="session")
@dataBaseDec(json_data,"mysql")
def mysqlConnect_sydr():
    ...

# cpa链接sqlite数据库
@pytest.fixture(scope="session")
@sqliteDec(filePath.sqlite_path + "DataByInsert.db")
def insertSqliteCon():
    ...

# 测试数据准备,在CPA数据库插入数据
@dataSysDec("session",filePath.sqlite_path + "TablesJSON.json","cpaSydr","cpa")
def generateDataByInsert(mysqlConnect_sydr,insertSqliteCon):
    ...


#sqlite连接测试固件
@pytest.fixture(scope="session")
@sqliteDec(filePath.sqlite_path+"sydr_DB.db")
def sqliteConnect():
    ...

@pytest.fixture(scope="session")
def androidAndWebSession(sqliteConnect:Connection):
    cur=sqliteConnect.cursor()
    cur.execute('select phone from user_info where tag="base"')
    phone=cur.fetchone()[0]
    androidSession=getLoginSession("android",phone,"app")
    webSession=getLoginSession("web",phone,"web")
    sessionList=[]
    CatchRequest("a","a",{"a":2},"2",{"b":3},"dd")
    sessionList.append(androidSession)
    sessionList.append(webSession)
    sessionList.append(phone)
    yield sessionList
    from utils.common_sydr import sessionList
    for session in sessionList:
        delete_user_mid(int(session.headers.get("userId")))



# wework数据库连接，和数据创建
@pytest.fixture(scope="session")
@dataBaseDec(json_data,"mysql",project="wework")
def weworkMysqlCon():
    ...

@pytest.fixture(scope="session")
@sqliteDec(filePath.sqlite_path+"wework.db")
def weworkSqliteCon():
    ...

@dataSysDec("session",filePath.sqlite_path + "weworkbase.json","cpaSydr","wework")
def weworkDataInsert(weworkMysqlCon,weworkSqliteCon):
    ...

# 商城数据库连接，和数据创建
@pytest.fixture(scope="session")
@dataBaseDec(json_data,"mysql",project="shop")
def shopMysqlCon():
    ...

@pytest.fixture(scope="session")
@sqliteDec(filePath.sqlite_path+"shop.db")
def shopSqliteCon():
    ...


@dataSysDec("session",filePath.sqlite_path + "shop.json","cpaSydr","shop",logTables=["goods_order","goods_order_sku"])
def shopDataInsert(shopMysqlCon,shopSqliteCon):
    ...


# 直播数据创建

@pytest.fixture(scope="session")
@dataBaseDec(json_data,"mysql",project="live")
def liveMysqlCon():
    ...

@pytest.fixture(scope="session")
@sqliteDec(filePath.sqlite_path+"live.db")
def liveSqliteCon():
    ...


@dataSysDec("session",filePath.sqlite_path + "liveJson.json","cpaSydr","live")
def liveDataByInsert(liveMysqlCon,liveSqliteCon):
    ...



@pytest.fixture(scope="session")
def scheduleReq():
    s:Session=requests.session()
    h={
        "mode":"debug"
    }
    s.headers.update(h)
    yield s






@pytest.fixture(scope="session")
@sqliteDec(filePath.redisSqlite_path+"redisDataCpaSydr.db")
def cpaRedisSqliteCon():
    ...


@redisDataSysDec("session","cpa_sydr")
def redisInsert(cpaRedisSqliteCon,mysqlConnect_sydr,redisConnect):
    ...





if __name__=="__main__":
    # mysql_json = json_data.get(json_data.get("current_env"))
    # server = mysql_json.get("server")
    # port = int(mysql_json.get("port"))
    # user = mysql_json.get("username")
    # passwd = mysql_json.get("password")
    # database = mysql_json.get("database")
    # db = pymysql.connect(host=server, port=port, user=user, password=passwd, db=database, charset='utf8',
    #                      autocommit=True)
    # cursor = db.cursor()
    #
    # redis_json = json_data.get(json_data.get("current_env")).get("redis")
    # server = redis_json.get("server")
    # port = redis_json.get("port")
    # passwd = redis_json.get("passwd")
    # db = redis_json.get("db")
    # redis = Redis(host=server, port=port, db=db, password=passwd, decode_responses=True, socket_keepalive=True)

    # sqliteCon = sqlite3.connect(filePath.redisSqlite_path+"redisDataCpaSydr.db")
    # RedisInsert.createSqliteTable(sqliteCon)
    s=""
    s[:-1]
    print(s)
